package me.ratel.demo.eBook.infrastructure.persistence.hibernate;

import me.ratel.demo.eBook.domain.model.ModelConstants;
import me.ratel.demo.eBook.domain.model.user.IUserDao;
import me.ratel.demo.eBook.domain.model.user.User;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author zhouyaming
 * @time 2016/6/12 22:32
 */
@Repository
public class UserDao extends GenericDao<User, Integer> implements IUserDao {
    public UserDao() {
        super(User.class);
    }

    @Override public List<User> list(Map<String, Object> condition, Order order) {
        Criteria criteria = this.getSession().createCriteria(User.class);

        if (StringUtils.isNoneBlank((String)condition.get("openId"))) {
            criteria.add(Restrictions.eq("openId", condition.get("openId")));
        }

        criteria.addOrder(order == null ? Order.asc("id"): order);

        criteria.add(Restrictions.eq("deleteFlag", ModelConstants.DeleteFlag.NO.getValue()));

        return criteria.list();
    }
}
